package com.starsoft.frame.base.web;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.starsoft.frame.base.bean.BaseAcount;
import com.starsoft.frame.base.bean.BaseUser;
import com.starsoft.frame.base.bean.UserContext;
import com.starsoft.frame.base.service.UserService;
import com.starsoft.frame.core.web.WebUtil;
import com.starsoft.frame.util.StringUtil;

@RestController
@RequestMapping(value = "/base")
public class LoginController {
	@Autowired
	private UserService us;

	@RequestMapping(value = "/login/login")
	public ModelAndView login(HttpServletRequest request, HttpServletResponse response, ModelAndView mv) {
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String remPwd = request.getParameter("remPwd");

		BaseUser user = us.checkUser(userName, userPwd);
		if (user != null) {
			UserContext userContext = us.getUserContext(userName);
			setAcountFromCookie(request);
			request.getSession().setAttribute("user", userContext);
			mv.setViewName("redirect:/");

			if ("on".equals(remPwd)) {
				// 设置cookie
				Cookie c1 = new Cookie("userName", userName);
				Cookie c2 = new Cookie("userPwd", userPwd);
				Cookie c3 = new Cookie("acount", userContext.getAcountId() + "");
				c1.setMaxAge(30 * 24 * 60 * 60);
				c2.setMaxAge(30 * 24 * 60 * 60);
				c3.setMaxAge(30 * 24 * 60 * 60);
				c1.setPath(request.getContextPath());
				c2.setPath(request.getContextPath());
				c3.setPath(request.getContextPath());
				response.addCookie(c1);
				response.addCookie(c2);
				response.addCookie(c3);
			}
		} else {
			mv.addObject("error", "用户或密码不正确");
			mv.setViewName("redirect:/base/login/login.jsp");
		}
		return mv;
	}

	private void setAcountFromCookie(HttpServletRequest request) {
		for (Cookie cookie : request.getCookies()) {
			if (cookie.getName().equals("acount")) {
				try {
					String acountIdStr = cookie.getValue();
					if (StringUtil.isEmpty(acountIdStr)) {
						Integer acountId = Integer.parseInt(acountIdStr);
						BaseAcount acount = us.getAcount(acountId);
						UserService.getUserContext().setAcount(acount);
					}
					break;
				} catch (Exception e) {
					// TODO: handle exception
				}
			}
		}
	}

	@RequestMapping("/loginuser/changeacount")
	public Map<String, Object> changeAcount(HttpServletRequest request, HttpServletResponse response, BaseAcount acount) {
		acount = us.getAcount(acount.getAcountId());
		UserService.getUserContext().setAcount(acount);
		// 设置cookie
		Cookie c3 = new Cookie("acount", acount.getAcountId() + "");
		c3.setMaxAge(7 * 24 * 60 * 60);
		c3.setPath(request.getContextPath());
		response.addCookie(c3);
		return WebUtil.sucesseModel("");
	}

	@RequestMapping(value = "/login/logout")
	private ModelAndView logout(HttpServletRequest request, HttpServletResponse response) {
		Cookie c1 = new Cookie("userName", "");
		c1.setMaxAge(0);
		c1.setPath(request.getContextPath());
		Cookie c2 = new Cookie("userPwd", "");
		c2.setMaxAge(0);
		c2.setPath(request.getContextPath());
		response.addCookie(c1);
		response.addCookie(c2);
		request.getSession().invalidate();
		return new ModelAndView("redirect:/");
	}

	@RequestMapping(value = "/login/register")
	public ModelAndView register(HttpServletRequest request, ModelAndView mv) {
		String userName = request.getParameter("userName");
		String userFullname = request.getParameter("userFullname");
		String userPwd = request.getParameter("userPwd");
		String userPwd2 = request.getParameter("userPwd2");
		String role = request.getParameter("role");

		mv.setViewName("redirect:/");
		if (!userPwd.equals(userPwd2)) {
			mv.addObject("error", "两次密码输入不同");
			mv.setViewName("/base/index/register");
		} else {
			BaseUser user = us.getUser(userName);
			if (user != null) {
				mv.addObject("error", "用户名已被占用");
				mv.setViewName("/base/index/register");
			} else {
				user = new BaseUser(userName, userPwd2);
				user.setUserFullname(userFullname);
				if ("老师".equals(role)) {
					user.setRoleId(4);
				} else {
					user.setRoleId(3);
				}
				us.saveUser(user);
				UserContext userContext = us.getUserContext(userName);
				request.getSession().setAttribute("user", userContext);
			}
		}
		return mv;
	}

}
